import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { UserService } from './user.service';
import { UserController } from './user.controller';
import { MongooseModule } from '@nestjs/mongoose';
import { User, UserSchema } from 'src/schemas/user.schema';
import { HashPasswordMiddleware } from 'src/middleware/hash-password.middleware';
import { RequestMethod } from '@nestjs/common';

@Module({
  imports: [
    MongooseModule.forFeature([
      {
        name: User.name, // 这里的name就是User
        schema: UserSchema,
      },
    ]),
  ],
  controllers: [UserController],
  providers: [UserService],
})
export class UserModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(HashPasswordMiddleware) // 使用密码加密中间件
      .forRoutes({
        path: 'user',
        method: RequestMethod.POST,
      }); // 只对 user 路由的post请求起作用
  }
}
